package com.demo.autotest.controller;

import com.demo.autotest.common.utils.PageUtils;
import com.demo.autotest.database.domain.Role;
import com.demo.autotest.database.domain.User;
import com.demo.autotest.database.domain.UserRole;
import com.demo.autotest.dto.PageDTO;
import com.demo.autotest.dto.RoleDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.request.RoleUserRequest;
import com.demo.autotest.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // 定义一个controller类
@RequestMapping("/autotest/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping("/add/user")
    public void addRoleUser(@RequestBody RoleUserRequest request){
        roleService.addRoleUser(request);
    }

    @PostMapping("/user/delete")
    public void deleteRoleUser(@RequestBody UserRole userRole){
        roleService.deleteRoleUser(userRole);
    }

    @GetMapping("/user/list/{roleId}")
    public List<User> getRoleUser(@PathVariable String roleId){
        return roleService.getRoleUser(roleId);
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public PageDTO<List<RoleDTO>> getRoleList(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                              @RequestBody QueryRequest queryRequest, HttpServletRequest request){
        String user = request.getSession().getAttribute("userId").toString();
        queryRequest.setRequestUser(user);
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        return PageUtils.setPageInfo(page, roleService.getRoleList(queryRequest));
    }
}
